package com.atguigu.eduservice.controller.front;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.Result;
import com.atguigu.commonutils.entity.UcenterMemberVo;
import com.atguigu.eduservice.client.UcenterClient;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 前端的评论功能
 * </p>
 *
 * @author 董万均
 * @since 2020-11-07
 */
@RestController
@RequestMapping("/eduservice/comment")
public class CommentFrontController {

    @Autowired
    private EduCommentService commentService;

    @Autowired
    private UcenterClient ucenterClient;

    //根据课程分页查询
    @GetMapping("pageCommentCondition/{page}/{limit}")
    public Result pageCommentCondition(@PathVariable long page,@PathVariable long limit,String courseId){
        Page<EduComment> commentPage=new Page(page,limit);
        Map<String,Object> map=commentService.selectCommentPage(commentPage,courseId);
        return Result.ok().data(map);
    }

    //添加评论
    @PostMapping("addComment")
    public Result addComment(@RequestBody EduComment comment, HttpServletRequest request){
        //获取token中的token信息
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //从token中获取用户的信息
        UcenterMemberVo userInfo = ucenterClient.getUserInfo(memberId);
        //该数据库中设置登录信息中的id，昵称，头像
        comment.setMemberId(memberId);
        comment.setNickname(userInfo.getNickname());
        comment.setAvatar(userInfo.getAvatar());
        commentService.save(comment);
        return Result.ok();
    }

}

